package com.wfm.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wfm.commonutils.R;
import com.wfm.eduservice.entity.EduCourse;
import com.wfm.eduservice.entity.EduTeacher;
import com.wfm.eduservice.entity.vo.CoursePublishVo;
import com.wfm.eduservice.entity.vo.CourseInfoVo;
import com.wfm.eduservice.entity.vo.CourseQuery;
import com.wfm.eduservice.entity.vo.TeacherQuery;
import com.wfm.eduservice.service.EduCourseService;
import com.wfm.eduservice.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wfm
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "新增课程")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(
            @ApiParam(name = "CourseInfoVo", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVo courseInfoVo){
        String courseId = courseService.saveCourseInfo(courseInfoVo);
        if(!StringUtils.isEmpty(courseId)){
            return R.ok().data("courseId", courseId);
        }else{
            return R.error().message("保存失败");
        }
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping("courseInfo/{id}")
    public R getById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        CourseInfoVo courseInfoForm = courseService.getCourseInfoFormById(id);
        return R.ok().data("item", courseInfoForm);
    }
    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublish = courseService.getCoursePublishVoById(id);
        return R.ok().data("publishCourse",coursePublish);

    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publishCourse/{id}")
    public R publishCourseById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    @PostMapping("getCourseList")
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("items","list");
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("pageCourse/{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){
        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.page(pageParam, null);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false)CourseQuery courseQuery){
        Page<EduCourse> pageCourse = new Page<>(current,limit);

        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String name = courseQuery.getTitle();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("title", name);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        //降序排序
        queryWrapper.orderByDesc("gmt_create");
        //分页查询
        courseService.page(pageCourse, queryWrapper);
        List<EduCourse> records = pageCourse.getRecords();
        for(EduCourse eduCourse:records){
            String teacherId = eduCourse.getTeacherId();
            EduTeacher teacher = teacherService.getById(teacherId);
            String teacherName = teacher.getName();
            eduCourse.setTeacherId(teacherName);
        }
        long total = pageCourse.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }
    @PostMapping("pageCourseCondition2/{current}/{limit}")
    public R pageCourseCondition2(@PathVariable long current, @PathVariable long limit,
                                 @RequestBody(required = false)CourseQuery courseQuery){
        Page<EduCourse> pageCourse = new Page<>(current,limit);

        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        String name = courseQuery.getTitle();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }
        //降序排序
        queryWrapper.orderByDesc("gmt_create");
        //分页查询
        courseService.page(pageCourse, queryWrapper);
        List<EduCourse> records = pageCourse.getRecords();
        long total = pageCourse.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }
    @DeleteMapping("{id}")
    public R removeTeacher(@PathVariable String id){
        boolean b = courseService.removeById(id);
        if(b){
            return  R.ok();
        }else {
            return R.error();
        }
    }
}

